//
//  DYSmallPlayerView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/6.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

class DYSmallPlayerView: SYBaseView {

    var playOrPauseClosure: ((Bool)->Void)?
    var model: DYJQJIangjieListModel!
    let yuan: DYYuanxingProgress = {
        let v: DYYuanxingProgress = DYYuanxingProgress()
        
        return v
    }()
    
    
    lazy var detail: VGBaseTitleImgView = {
        let v: VGBaseTitleImgView = VGBaseTitleImgView(frame: CGRect.zero, type: .imgRight)
        v.textLb.text = "详情"
        v.textLb.font  =  12.autoFontSize()
        v.textLb.textColor = .white
        v.imageV.image = UIImage(named: "jiangjie_shang")
        v.tapClosure = {[weak self] (index, isSelect) in
            if let weakSelf = self {
                weakSelf.clickedClosure?(0)
            }
        }
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor.white
        v.font = 17.autoFontSize()
        return v
    }()
    
    let timeLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = .white
        v.font = 14.autoFontSize()
        return v
    }()
    
    let pic: UIImageView = {
        let v: UIImageView = UIImageView()
        
        v.layer.cornerRadius = 3
        v.layer.masksToBounds = true
        return v
    }()
    
    fileprivate var playOrPauseImg: UIImageView = {
        let v: UIImageView = UIImageView()
        v.sy_name("jiangjie_pause")
        return v
    }()
    
    lazy var playOrPauseBt: UIButton = {
        let bt: UIButton = UIButton()
        
        bt.addTarget(self, action: #selector(playOrPauseAction), for: .touchUpInside)
        return bt
    }()
    
    var playOrPauseBg: UIView = {
        let v = UIView()
        v.layer.cornerRadius = 19.0.autoWidth()
        v.backgroundColor = UIColor(hex: 0xFFA02A)
        return v
    }()

    
    func configure(_ model: DYJQJIangjieListModel) {
        self.model = model
        nameLb.text = model.title
        
        pic.sy_setWithUrl(model.imgUrl)
        
    }
    
    override func initViews() {
        self.backgroundColor = UIColor(white: 0, alpha: 0.9)
       
        
        addSubview(pic)
        addSubview(nameLb)
        addSubview(timeLb)
        addSubview(detail)
        addSubview(playOrPauseBg)
        playOrPauseBg.addSubview(yuan)
        addSubview(playOrPauseImg)
        addSubview(self.playOrPauseBt)
        
        pic.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15.0.autoWidth())
            make.bottom.equalToSuperview().offset(-24.0.autoWidth())
            make.left.equalToSuperview().offset(15.0.autoWidth())
            make.width.height.equalTo(51.0.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.left.equalTo(pic.snp.right).offset(8.5.autoWidth())
            make.centerY.equalTo(pic)
            make.height.equalTo(20.0.autoWidth())
        }
        timeLb.snp.makeConstraints { (make) in
            make.right.equalTo(playOrPauseBt.snp.left).offset(-6.5.autoWidth())
            make.centerY.equalTo(pic)
            make.height.equalTo(20.0.autoWidth())
        }
        detail.snp.makeConstraints { (make) in
            make.left.equalTo(nameLb.snp.right).offset(11.5.autoWidth())
            make.centerY.equalTo(pic)
            make.height.equalTo(30.0.autoWidth())
        }
        playOrPauseBg.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-15.5.autoWidth())
            make.centerY.equalTo(pic)
            make.width.height.equalTo(38.0.autoWidth())
        }
        self.playOrPauseBt.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-15.5.autoWidth())
            make.centerY.equalTo(pic)
            make.width.height.equalTo(38.0.autoWidth())
        }
        self.playOrPauseImg.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-29.5.autoWidth())
            make.centerY.equalTo(pic)
            make.width.equalTo(11.0.autoWidth())
            make.height.equalTo(14.0.autoWidth())
        }
        self.yuan.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    @objc func playOrPauseAction(sender: UIButton) {
        
        sender.isSelected = !sender.isSelected
        let vc = self.currentVC as! DYJiangjieListViewController
        vc.isPause = sender.isSelected
        self.playState(sender: sender)
        if let closure = playOrPauseClosure {
            closure(sender.isSelected)
        }
    }
    
    func setPlayState(play: Bool) {
        self.playOrPauseBt.isSelected = !play
        self.playState(sender: playOrPauseBt)
    }
    
    fileprivate func playState(sender: UIButton) {
        
        if sender.isSelected {
            playOrPauseImg.sy_name("jiangjie_play")

            self.playOrPauseImg.snp.remakeConstraints { (make) in
                make.right.equalTo(self).offset(-27.5.autoWidth())
                make.centerY.equalTo(pic)
                make.width.equalTo(11.0.autoWidth())
                make.height.equalTo(14.0.autoWidth())
            }
        } else {
            playOrPauseImg.sy_name("jiangjie_pause")

            self.playOrPauseImg.snp.remakeConstraints { (make) in
                make.right.equalTo(self).offset(-29.5.autoWidth())
                make.centerY.equalTo(pic)
                make.width.equalTo(11.0.autoWidth())
                make.height.equalTo(14.0.autoWidth())
            }
        }
        
    }
}
